/**
 * @filename RoleService.java
 * @package com.tianjian.dass.system.service
 * @description TODO
 * @author 胡正衛
 * @date 2012-07-15 21:45:37
 * @version v0.1
 */
package com.tianjian.oa.system.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianjian.common.Criteria;
import com.tianjian.oa.system.mapper.RoleMapper;
import com.tianjian.oa.system.pojos.RoleInfo;

/**
 * @author 胡正衛
 */
@Service
public class RoleService {

	@Autowired
	private RoleMapper roleMapper;

	/**
	 * 获取列表
	 * @param criteria
	 * @return
	 */
	public List<RoleInfo> getData(Criteria criteria) {
		try {
			return roleMapper.getData(criteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取总数
	 * @param criteria
	 * @return
	 */
	public int getCount(Criteria criteria) {
		try {
			return roleMapper.getCount(criteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 获取用户权限树列表
	 * @param criteria
	 * @param all 获取全部,true:拥有权限标识为checked, false:只获取该用户所拥有的权限
	 * @return
	 */
	public List<RoleInfo> getRole(int userid, boolean all) {
		try {
			Criteria criteria = new Criteria();
			criteria.put("userid", userid);
			criteria.put("all", all);
			return roleMapper.getRole(criteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 设置用户权限树列表
	 * @param criteria
	 * @return
	 */
	public int setRole(int userid, int[] roleid) {
		try {
			Criteria criteria = new Criteria();
			criteria.put("userid", userid);
			criteria.put("roleid", roleid);
			roleMapper.setRole(criteria);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	/**
	 * 添加用户
	 * @return
	 */
	public int add(RoleInfo role) {
		try {
			return roleMapper.insert(role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 编辑
	 * @return
	 */
	public int edit(RoleInfo role) {
		try {
			return roleMapper.update(role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 删除
	 * @return
	 */
	public int del(String id) {
		try {
			return roleMapper.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public int insertAdmin(int roleid, int menuid) {
		try {
			Criteria criteria = new Criteria();
			criteria.put("roleid", roleid);
			criteria.put("menuid", menuid);
			return roleMapper.insertAdmin(criteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
}
